package cn.janescott.framework.druid.properties;

import cn.janescott.framework.core.annotation.RequiredProperty;
import com.alibaba.druid.support.http.ResourceServlet;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.ArrayList;
import java.util.List;

import static cn.janescott.framework.core.utils.AnnotationUtils.checkRequired;

/**
 * servlet监控配置
 * 只是添加了常用的一些配置
 * 如果有其余配置，可以参考
 *
 * @author scott
 * @date 2019/2/18
 * @see WebStatFilter
 * @see StatViewServlet
 * @see ResourceServlet
 */
@ConfigurationProperties(prefix = "druid.monitor", ignoreUnknownFields = false)
public class MonitorProperties implements InitializingBean {
    @RequiredProperty("monitor")
    private boolean monitor = false;

    @RequiredProperty("servletName")
    private String servletName = "statServlet";

    @RequiredProperty("path")
    private String path = "/druid/*";

    private String allow;

    private String deny;

    @RequiredProperty("loginUsername")
    private String loginUsername = "root";

    @RequiredProperty("loginPassword")
    private String loginPassword = "root";

    private String jmxUrl;

    private String jmxUsername;

    private String jmxPassword;

    @RequiredProperty("resetEnable")
    private boolean resetEnable = false;
    @RequiredProperty("initialCheck")
    private boolean initialCheck = false;
    @RequiredProperty("failedOnCheck")
    private boolean failedOnCheck = false;

    private List<String> jmxUrls = new ArrayList<>();

    private List<String> jmxUserNames = new ArrayList<>();

    private List<String> jmxPassWords = new ArrayList<>();

    private List<String> jmxDruidNames = new ArrayList<>();

    @RequiredProperty("filterName")
    private String filterName = "druidFilter";

    @RequiredProperty("pattern")
    private String pattern = "/*";

    @RequiredProperty("exclusions")
    private String exclusions = "*.js,*.gif,*.jpg,*.png,*.css,*.ico";

    public boolean isMonitor() {
        return monitor;
    }

    public void setMonitor(boolean monitor) {
        this.monitor = monitor;
    }

    public String getServletName() {
        return servletName;
    }

    public void setServletName(String servletName) {
        this.servletName = servletName;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getAllow() {
        return allow;
    }

    public void setAllow(String allow) {
        this.allow = allow;
    }

    public String getDeny() {
        return deny;
    }

    public void setDeny(String deny) {
        this.deny = deny;
    }

    public String getLoginUsername() {
        return loginUsername;
    }

    public void setLoginUsername(String loginUsername) {
        this.loginUsername = loginUsername;
    }

    public String getLoginPassword() {
        return loginPassword;
    }

    public void setLoginPassword(String loginPassword) {
        this.loginPassword = loginPassword;
    }

    public String getJmxUrl() {
        return jmxUrl;
    }

    public void setJmxUrl(String jmxUrl) {
        this.jmxUrl = jmxUrl;
    }

    public String getJmxUsername() {
        return jmxUsername;
    }

    public void setJmxUsername(String jmxUsername) {
        this.jmxUsername = jmxUsername;
    }

    public String getJmxPassword() {
        return jmxPassword;
    }

    public void setJmxPassword(String jmxPassword) {
        this.jmxPassword = jmxPassword;
    }

    public boolean isResetEnable() {
        return resetEnable;
    }

    public void setResetEnable(boolean resetEnable) {
        this.resetEnable = resetEnable;
    }

    public boolean isInitialCheck() {
        return initialCheck;
    }

    public void setInitialCheck(boolean initialCheck) {
        this.initialCheck = initialCheck;
    }

    public boolean isFailedOnCheck() {
        return failedOnCheck;
    }

    public void setFailedOnCheck(boolean failedOnCheck) {
        this.failedOnCheck = failedOnCheck;
    }

    public List<String> getJmxUrls() {
        return jmxUrls;
    }

    public void setJmxUrls(List<String> jmxUrls) {
        this.jmxUrls = jmxUrls;
    }

    public List<String> getJmxUserNames() {
        return jmxUserNames;
    }

    public void setJmxUserNames(List<String> jmxUserNames) {
        this.jmxUserNames = jmxUserNames;
    }

    public List<String> getJmxPassWords() {
        return jmxPassWords;
    }

    public void setJmxPassWords(List<String> jmxPassWords) {
        this.jmxPassWords = jmxPassWords;
    }

    public List<String> getJmxDruidNames() {
        return jmxDruidNames;
    }

    public void setJmxDruidNames(List<String> jmxDruidNames) {
        this.jmxDruidNames = jmxDruidNames;
    }

    public String getFilterName() {
        return filterName;
    }

    public void setFilterName(String filterName) {
        this.filterName = filterName;
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public String getExclusions() {
        return exclusions;
    }

    public void setExclusions(String exclusions) {
        this.exclusions = exclusions;
    }

    /**
     * 增加必填属性验证
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        checkRequired(this.getClass(), this);
    }
}
